﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Linq.Expressions;
using System.Windows;
using System.Windows.Data;
using System.Reflection;

namespace Inspire.Utility
{
    public static class ExtensionMethods
    {
        /// <summary>
        /// Provides a utility method for obtaining the next ID number from an ObjectSet collection.
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="table"></param>
        /// <param name="selector"></param>
        /// <returns></returns>
        public static TResult NextId<TSource, TResult>(this ObjectSet<TSource> table, Expression<Func<TSource, TResult>> selector) where TSource : class
        {
            TResult lastId = table.Any() ? table.Max(selector) : default(TResult);

            if (lastId is int)
            {
                lastId = (TResult)(object)(((int)(object)lastId) + 1);
            }

            return lastId;
        }
    }
}
